﻿using System;
using System.Threading.Tasks;
using DDD.Simple.DomainBase;

namespace Dealer.Domain.AggregateModels.DealerAggregate
{
    /// <summary>
    /// Dealer tree.
    /// </summary>
    public class DealerTree
        : IValueObject
    {
        private readonly IDealerRepository _dealerRepository;
        public DealerTree(IDealerRepository dealerRepository)
        {
            _dealerRepository = dealerRepository;
        }
        public Guid Id { get; set; }
        public Guid DealerId { get; set; }
        public Guid? ParentDealerId { get; set; }
        public int Layer { get; set; }

        public async Task<DealerTree> CreateDealerTreeAsync(Guid dealerId, Guid? parentDealerId)
        {
            Id = Guid.NewGuid();
            DealerId = dealerId;
            ParentDealerId = parentDealerId;
            Layer = parentDealerId == null ? 1 : (await _dealerRepository.GetDealerLayerAsync(parentDealerId.Value)) + 1;
            return this;
        }
    }
}